package com.zhang.management;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zhang.bean.Course;

import zhang.utils.JDBCUtils;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class KeChengGuanLi extends JFrame {

	private JPanel contentPane;
	private JTextField tname;
	private JTextField tgrade;
	private JDBCUtils jdb = new JDBCUtils();
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField tselect;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					KeChengGuanLi frame = new KeChengGuanLi();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public KeChengGuanLi() {
		setDefaultCloseOperation(JFrame.TEXT_CURSOR);
		setBounds(100, 100, 523, 340);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("课程管理");
		lblNewLabel.setFont(new Font("黑体", Font.PLAIN, 32));
		lblNewLabel.setBounds(180, 10, 145, 58);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("课程名称：");
		lblNewLabel_1.setBounds(51, 78, 78, 15);
		contentPane.add(lblNewLabel_1);
		
		tname = new JTextField();
		tname.setBounds(124, 78, 120, 21);
		contentPane.add(tname);
		tname.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("课程学分：");
		lblNewLabel_2.setBounds(287, 78, 69, 15);
		contentPane.add(lblNewLabel_2);
		
		tgrade = new JTextField();
		tgrade.setBounds(366, 75, 91, 28);
		contentPane.add(tgrade);
		tgrade.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("课程性质：");
		lblNewLabel_3.setBounds(51, 114, 69, 15);
		contentPane.add(lblNewLabel_3);
		
		JRadioButton rbixiu = new JRadioButton("必修");
		buttonGroup.add(rbixiu);
		rbixiu.setBounds(122, 110, 58, 23);
		contentPane.add(rbixiu);
		
		JRadioButton rxuanxiu = new JRadioButton("选修");
		buttonGroup.add(rxuanxiu);
		rxuanxiu.setBounds(200, 110, 64, 23);
		contentPane.add(rxuanxiu);
		
		JLabel lblNewLabel_4 = new JLabel("课程介绍");
		lblNewLabel_4.setBounds(49, 151, 58, 15);
		contentPane.add(lblNewLabel_4);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(59, 176, 398, 77);
		contentPane.add(scrollPane);
		
		JTextArea tintroduce = new JTextArea();
		scrollPane.setViewportView(tintroduce);
		
		JButton insertButton = new JButton("新增");
		insertButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				//true 必修 false 选修
				boolean tmp = rbixiu.isSelected()==true?true:false;
				Course course = new Course(tname.getText(), Integer.parseInt(tgrade.getText()), tmp,tintroduce.getText());
				jdb.insert(course);
				
			}
		});
		insertButton.setBounds(47, 270, 97, 23);
		contentPane.add(insertButton);
		
		JButton deleteButton = new JButton("删除");
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Course course = new Course();
				course.setid(Integer.parseInt(tselect.getText()));
				jdb.delete(course);
			}
		});
		deleteButton.setBounds(169, 270, 97, 23);
		contentPane.add(deleteButton);
		
		tselect = new JTextField();
		tselect.setBounds(287, 272, 66, 21);
		contentPane.add(tselect);
		tselect.setColumns(10);
		
		JButton readButton = new JButton("查找");
		readButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Course course = new Course();
				course.setid(Integer.parseInt(tselect.getText()));
				Course selectOne = jdb.selectOne(Course.class,course.getid());
				tname.setText(selectOne.getName());
			}
		});
		readButton.setBounds(366, 270, 97, 23);
		contentPane.add(readButton);
		this.setVisible(true);
	}
}
